<?php
/**
 * Created by PhpStorm.
 * User: pc
 * Date: 2024/3/14
 * Time: 16:31
 */
namespace app\modules\bg\controllers;
use app\common\enum\SetUrlCrawler_statusEnum;
use app\common\enum\SetUrlCrawler_urlUserEnum;
use app\common\service\GptCrawler;
use app\common\service\MyJob;
use app\common\service\Running;
use app\models\SetRssSitemapCrawler;
use app\models\SetUrlCrawler;
use yii\base\Exception;
use yii\console\Controller;
use yii\db\Expression;
use yii\queue\Queue;

class IndexController extends Controller
{
    /**
     * @var Running
     */
    public $running_obj;
    public function beforeAction($action)
    {
        $this->running_obj = new Running(\Yii::$app->controller->action->id);
        if($this->running_obj->isRunning()){
//            echo "任务已执行";
            return false;
        }
        $this->running_obj->runStart();
        return parent::beforeAction($action); // TODO: Change the autogenerated stub
    }

    public function afterAction($action, $result)
    {
        $this->running_obj->runEnd();
        return parent::afterAction($action, $result); // TODO: Change the autogenerated stub
    }

    //测试： php yii /bg/index/step1 e=local.crawler.jinpinh.com
    // cd /www/wwwroot/gpt_crawler/api &&  php yii /bg/index/step1
    //后台请求api,第一步
    public function actionStep1(){
        $obj = new GptCrawler();
        $query = SetUrlCrawler::find()->andWhere(['status'=>SetUrlCrawler_statusEnum::UNDO])->limit(1);
        foreach ($query->batch(10) as $batch){
            foreach ($batch as $row){
                /**
                 * @var $row SetUrlCrawler
                 */
                try{
                    $obj->run($row);
                }catch (\Throwable $e){
                    $row->status = SetUrlCrawler_statusEnum::FAIL;
                    $row->err_msg = $e->getMessage();
                    $row->save();
                }
            }
        }
    }

    // 清除已使用的数据,规则：
    // 1.采集结果已使用且超过一个月：30天
    // 2.采集结果超过6个月:180天（不管是否使用）
    //测试： php yii /bg/index/clear e=local.crawler.jinpinh.com
    // cd /www/wwwroot/gpt_crawler/api &&  php yii /bg/index/clear
    public function actionClear(){
        $obj = new GptCrawler();

        //2.采集结果超过6个月:180天（不管是否使用）
        $six_month_before = date("Y-m-d",strtotime("-180 day"));
        $query = SetUrlCrawler::find()->andWhere(['<','created_at',$six_month_before]);
        foreach ($query->batch(10) as $batch){
            foreach ($batch as $row){
                /**
                 * @var $row SetUrlCrawler
                 */
                try{
                    $obj->clear($row);
                }catch (\Throwable $e){
                }
            }
        }

        // 1.采集结果已使用且超过一个月：30天
        $before_30 = date("Y-m-d",strtotime("-30 day"));
        $query = SetUrlCrawler::find()->andWhere(['<','created_at',$before_30])->andWhere(['url_user'=>SetUrlCrawler_urlUserEnum::USED]);
        foreach ($query->batch(10) as $batch){
            foreach ($batch as $row){
                /**
                 * @var $row SetUrlCrawler
                 */
                try{
                    $obj->clear($row);
                }catch (\Throwable $e){
                }
            }
        }
    }


    //测试： php yii /bg/index/rss e=local.crawler.jinpinh.com
    // cd /www/wwwroot/gpt_crawler/api &&  php yii /bg/index/rss
    public function actionRss(){
        $obj = new GptCrawler();

        $day_befor = date('Y-m-d H:i:s',strtotime("-1 day"));
        $query = SetRssSitemapCrawler::find()->andWhere([
            'or',
            ['status'=>SetUrlCrawler_statusEnum::UNDO],
            [
                'and',
                ['status'=>SetUrlCrawler_statusEnum::SUCCESS],
                ['<=','last_at',$day_befor],
            ],
        ])->limit(10);

        echo $query->createCommand()->rawSql."\n";

        foreach ($query->batch(10) as $batch){
            foreach ($batch as $row){
                /**
                 * @var $row SetRssSitemapCrawler
                 */
                try{
                    $obj->rss($row);
                }catch (\Throwable $e){
                    $row->status = SetUrlCrawler_statusEnum::FAIL;
                    $row->save();
                }
            }
        }
    }
}